/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.toolkit.cdr.impl;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import org.apache.log4j.Logger;

import who.tmap.bo.cd.CDProportionalSymbols;
import who.tmap.bo.cd.CDShapes;
import who.tmap.bo.cd.CartographicDisplay;
import who.tmap.common.TMapException;
import who.tmap.toolkit.MappingToolKit;
import who.tmap.toolkit.cdr.ProportionalSymbolsRepresentation;
import who.tmap.toolkit.impl.ShapeHelper;
import who.tmap.toolkit.shape.MapShape;
import who.tmap.toolkit.shape.MapShapes;
import who.tmap.toolkit.shape.PolygonShape;
import who.tmap.toolkit.shape.annotation.impl.ImageHelper;
import who.tmap.toolkit.shape.cdpointsymbol.ImageCDPointSymbol;
import who.tmap.toolkit.shape.impl.MapShapeFactory;

public class CDRProportionalSymbolsImpl extends BaseCDRImpl 
										implements ProportionalSymbolsRepresentation {

    private static Logger logger = Logger.getLogger(CDRProportionalSymbolsImpl.class); 	
    private ShapeHelper shapeHelper =new ShapeHelper();
    private CDRPSHelper psHelper =new CDRPSHelper(); 
    private ImageHelper imageHelper =new ImageHelper();
    private CDProportionalSymbols ps =null;
    private MapShapeFactory msf =MapShapeFactory.getInstance();
	
	public void init( MappingToolKit mtk, CartographicDisplay cd, double mapFactor ) throws TMapException {
		super.init( mtk, cd, mapFactor );
		
		processRanges( cd );
		
		psHelper.setMinVal( minVal );
		psHelper.setMaxVal( maxVal );
		psHelper.setMapFactor( mapFactor );
		
		ps =(CDProportionalSymbols)cd;
		
		if ( ps.getShape() == CDShapes.BITMAP ) {
			imageHelper =new ImageHelper();
			imageHelper.reset();

			imageHelper.setImage( ps.getImage() );
		}

	}
	
	public double getMaxVal() {
		return maxVal;
	}

	public double getMinVal() {
		return minVal;
	}  
	
	/**
	 * Draw a symbol for cd representation
	 * @param g
	 * @param mapShape
	 * @param val
	 * @param cdShape
	 * @param color
	 * @return
	 * @throws TMapException
	 */
	protected Rectangle2D draw( Graphics2D g, Rectangle2D bounds, double size, int cdShape, Color color, int fillPattern ) throws TMapException {
		Shape shape =shapeHelper.makeShape( new Point2D.Double( bounds.getCenterX(), bounds.getCenterY() ), size, size, cdShape, true );
		
		// Special case with no color :
		// It seems Area does not take stroke width into account
		// It is probably a Java2D issue
		if ( color == null && fillPattern == 0 ) {
			g.draw( shape );
		} else {
			drawShape( g, shape, color != null ? color : mtk.getBackgroundColor(), fillPattern );
		}
		
		return shape.getBounds2D();
	}	
    
	public void represent( Graphics2D g, MapShape mapShape ) throws TMapException {
			
		double symbolSize = psHelper.computeSymbolSize( getDoubleVal( mapShape ), ps.getScalingFactor() );

		Rectangle2D psSymbolBounds =null;
		
		if ( ps.getShape() != CDShapes.BITMAP ) {
			psSymbolBounds =draw( g, mapShape.getBounds(), symbolSize
				   , ps.getShape(), ps.getColor(), ps.getFillPattern() );
		} else {
			psSymbolBounds =drawBitmap( g, new Point2D.Double( mapShape.getBounds().getCenterX(), mapShape.getBounds().getCenterY() )
						 , symbolSize );
		}
		
		((PolygonShape)mapShape).setPsSymbolBounds( psSymbolBounds );
	}
	
    private Rectangle2D drawBitmap(Graphics2D g, Point2D pt, double symbolSize ) throws TMapException {
		ImageCDPointSymbol shape =(ImageCDPointSymbol)msf.getGraphic( MapShapes.CDPOINT_IMAGE );
		shape.setImage( imageHelper.getImage() );
		shape.setPt( pt );
		
		shape.setWidth( symbolSize * mapFactor );
		shape.setHeight( symbolSize * mapFactor );
		
		shape.setMapFactor( mapFactor );
		shape.setZoomFactor( mtk.getZoomFactor() );
		
		shape.draw( g );
		
		return shape.getBounds();
	}

}
